home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / xbmc-9.11.exe / scripts / TWC Supplemental / resources / lib / gui.py < prev    next >
Encoding:
Python Source  |  2009-06-26  |  26.1 KB  |  519 lines

  1. """
  2.     GUI for displaying maps and forecasts from weather.com
  3.     
  4.     Nuka1195
  5. """
  6.  
  7. # main imports
  8. import sys
  9. import os
  10.  
  11. import xbmc
  12. import xbmcgui
  13.  
  14. _ = xbmc.Language( os.getcwd() ).getLocalizedString
  15.  
  16. dialog = xbmcgui.DialogProgress()
  17. dialog.create( _( 0 ), _( 10 ) )
  18.  
  19. from threading import Timer
  20.  
  21. import resources.lib.TWCClient as TWCClient
  22.  
  23. # determine users preference window or dialog
  24. if ( sys.modules[ "__main__" ].window ):
  25.     module = xbmcgui.WindowXML
  26. else:
  27.     module = xbmcgui.WindowXMLDialog
  28.  
  29.  
  30. class GUI( module ):
  31.     # constants
  32.     ACTION_EXIT_SCRIPT = ( 10, )
  33.     ACTION_SET_DEFAULT = ( 117, )
  34.     ACTION_TOGGLE_MAP = ( 18, )
  35.     ACTION_MOVEMENT_LEFT = ( 1, )
  36.     ACTION_MOVEMENT_RIGHT = ( 2, )
  37.     # required control id's
  38.     CONTROL_MAP_LISTS = ( 500, 501, 502, )
  39.     CONTROL_HOUR_LIST = 600
  40.     CONTROL_10DAY_LIST = 700
  41.     # view buttons
  42.     CONTROL_MAP_BUTTON = 200
  43.     CONTROL_36HOUR_BUTTON = 201
  44.     CONTROL_HOURBYHOUR_BUTTON = 202
  45.     CONTROL_WEEKEND_BUTTON = 203
  46.     CONTROL_10DAY_BUTTON = 204
  47.     CONTROL_SETTINGS_BUTTON = 205
  48.     CONTROL_ALERTS_BUTTON = 206
  49.     # toggle buttons
  50.     CONTROL_WEEKEND_TOGGLE_BUTTON = 300
  51.     CONTROL_MAP_TOGGLE_BUTTON = 301
  52.     CONTROL_PLAY_VIDEO_BUTTON = 302
  53.     # settings buttons
  54.     CONTROL_ANIMATED_SETTING_BUTTON = 400
  55.     CONTROL_METRIC_SETTING_BUTTON = 401
  56.     CONTROL_SHOW_ALERTS_SETTING_BUTTON = 402
  57.     CONTROL_MAPS_LIST_SETTING_BUTTONS = ( 403, 404, 405, )
  58.     CONTROL_FANART_SETTING_BUTTON = 406
  59.     CONTROL_FANART_TYPE_SETTING_BUTTON = 407
  60.     CONTROL_FANART_DIFFUSE_SETTING_BUTTON = 408
  61.  
  62.     def __init__( self, *args, **kwargs ):
  63.         module.__init__( self, *args, **kwargs )
  64.         # set our defaults
  65.         self._init_defaults()
  66.         self._init_view_status()
  67.         # set the maps path
  68.         self._set_maps_path()
  69.         # get our local code, needed for localizing radars
  70.         self._get_local_code()
  71.         # get our new TWCClient
  72.         self._get_client()
  73.         # set default map list settings
  74.         self._set_default_maplists()
  75.  
  76.     def onInit( self ):
  77.         dialog.close()
  78.         # get default view
  79.         self._get_default_view()
  80.         # get current window
  81.         self._get_current_window()
  82.         # set script info
  83.         self._set_script_info()
  84.         # set default view
  85.         if ( self.defaultview == self.CONTROL_36HOUR_BUTTON ):
  86.             # get our 36 hour forecast
  87.             self._fetch_36_forecast()
  88.         elif ( self.defaultview == self.CONTROL_HOURBYHOUR_BUTTON ):
  89.             # get our hour by hour forecast
  90.             self._fetch_hour_forecast()
  91.         elif ( self.defaultview == self.CONTROL_WEEKEND_BUTTON ):
  92.             # get our hour by hour forecast
  93.             self._fetch_weekend_forecast()
  94.         elif ( self.defaultview == self.CONTROL_10DAY_BUTTON ):
  95.             # get our hour by hour forecast
  96.             self._fetch_10day_forecast()
  97.         else:
  98.             # set map list and get default map
  99.             self._fetch_map_list()
  100.  
  101.     def _get_current_window( self ):
  102.         # TODO: enable this code block if dialogs get properties
  103.         """
  104.         # current window for setting properties
  105.         self.CURRENT_WINDOW = xbmcgui.getCurrentWindowDialogId()
  106.         # not a valid dialog window, so grab the current window
  107.         if ( not( 13000 <= self.CURRENT_WINDOW <= 13100 ) ):
  108.         """
  109.         self.CURRENT_WINDOW = xbmcgui.Window( xbmcgui.getCurrentWindowId() )
  110.  
  111.     def _init_defaults( self ):
  112.         self.timer = None
  113.         self.toggle = True
  114.         self.weekendToggle = False
  115.         self.loading = False
  116.         self.maps_path = False
  117.         self.current_map = None
  118.  
  119.     def _set_default_maplists( self ):
  120.         if ( xbmc.getInfoLabel( "Skin.String(twc-maplist1-category)" ) == "" ):
  121.             xbmc.executebuiltin( "Skin.SetString(twc-maplist1-category,%s)" % ( self.TWCClient.BASE_MAPS[ 0 ][ 0 ], ) )
  122.             xbmc.executebuiltin( "Skin.SetString(twc-maplist1-title,%s)" % ( self.TWCClient.BASE_MAPS[ 0 ][ 2 ], ) )
  123.         if ( xbmc.getInfoLabel( "Skin.String(twc-maplist2-category)" ) == "" ):
  124.             xbmc.executebuiltin( "Skin.SetString(twc-maplist2-category,%s)" %  ( self.TWCClient.BASE_MAPS[ 2 ][ 0 ], ) )
  125.             xbmc.executebuiltin( "Skin.SetString(twc-maplist2-title,%s)" % ( self.TWCClient.BASE_MAPS[ 2 ][ 2 ], ) )
  126.         if ( xbmc.getInfoLabel( "Skin.String(twc-maplist3-category)" ) == "" ):
  127.             xbmc.executebuiltin( "Skin.SetString(twc-maplist3-category,%s)" % ( self.TWCClient.BASE_MAPS[ 37 ][ 0 ], ) )
  128.             xbmc.executebuiltin( "Skin.SetString(twc-maplist3-title,%s)" % ( self.TWCClient.BASE_MAPS[ 37 ][ 2 ], ) )
  129.  
  130.     def _set_script_info( self ):
  131.         self.CURRENT_WINDOW.setProperty( "version", "%s - r%s" % ( sys.modules[ "__main__" ].__version__, str( sys.modules[ "__main__" ].__svn_revision__ ) ) )
  132.         self.CURRENT_WINDOW.setProperty( "author", sys.modules[ "__main__" ].__author__ )
  133.         if ( self.defaultview == self.CONTROL_MAP_BUTTON ):
  134.             self.CURRENT_WINDOW.setProperty( "defaultview", "%s - %s" % ( _( self.defaultview ), xbmc.getInfoLabel( "Skin.String(twc-defaultmap)" ), ) )
  135.         else:
  136.             self.CURRENT_WINDOW.setProperty( "defaultview", _( self.defaultview ) )
  137.         self.CURRENT_WINDOW.setProperty( "svnurl", sys.modules[ "__main__" ].__url__ )
  138.         self.CURRENT_WINDOW.setProperty( "scripturl", sys.modules[ "__main__" ].__svn_url__ )
  139.  
  140.     def _init_view_status( self ):
  141.         self.forecast36Hour = None
  142.         self.forecastHourByHour = None
  143.         self.forecastWeekend = None
  144.         self.forecast10Day = None
  145.  
  146.     def _get_client( self ):
  147.         # setup our radar client
  148.         self.TWCClient = TWCClient.TWCClient( self.local_code )
  149.  
  150.     def _get_local_code( self ):
  151.         # use the Weather window property (should never return None)
  152.         self.local_code = xbmc.getInfoLabel( "Window(Weather).Property(AreaCode)" )
  153.  
  154.     def _set_maps_path( self, path=0 ):
  155.         # we have three possibilities. loading, default (error) or the actual map path
  156.         if ( path == 0 ):
  157.             xbmc.executebuiltin( "Skin.SetString(twc-mapspath,weather.com/loading)" )
  158.         elif ( path == 1 ):
  159.             xbmc.executebuiltin( "Skin.SetString(twc-mapspath,%s)" % ( self.maps_path, ) )
  160.         elif ( path == 2 ):
  161.             xbmc.executebuiltin( "Skin.SetString(twc-mapspath,weather.com/default)" )
  162.  
  163.     def _fetch_map_list( self ):
  164.         # only run this once
  165.         if ( self.current_map is None ):
  166.             ### clear our maplist exists properties
  167.             ##for count in range( len( self.CONTROL_MAP_LISTS ) ):
  168.             ##    self.CURRENT_WINDOW.clearProperty( "maplist%d" % ( count + 1, ) )
  169.             # initialize current map
  170.             self.current_map = 0
  171.             self.current_maplist = self.CONTROL_MAP_LISTS[ 0 ]
  172.             # get the users default map
  173.             default = xbmc.getInfoLabel( "Skin.String(twc-defaultmap)" )
  174.             map_lists = 1
  175.            # set number of map lists, only the US has multiple
  176.             map_lists = len( self.CONTROL_MAP_LISTS )
  177.             # enumurate thru map lists and fetch map list
  178.             for maplist_count in range( map_lists ):
  179.                 # what is users preference
  180.                 map = xbmc.getInfoLabel( "Skin.String(twc-maplist%d-category)" % ( maplist_count + 1, ) )
  181.                 # check for users preferemce
  182.                 for count, mapc in enumerate( self.TWCClient.BASE_MAPS ):
  183.                     # found it, no need to continue
  184.                     if ( mapc[ 0 ] == map ):
  185.                         break
  186.                 # set map list tab exists
  187.                 self.CURRENT_WINDOW.setProperty( "maplist%d" % ( maplist_count + 1, ), "1" )
  188.                 # fetch map list
  189.                 map_list = self.TWCClient.fetch_map_list( count )
  190.                 # lock the gui for faster updating
  191.                 xbmcgui.lock()
  192.                 # reset our map list
  193.                 self.getControl( self.CONTROL_MAP_LISTS[ maplist_count ] ).reset()
  194.                 # enumerate thru our map list and add map and title and check for default
  195.                 for count, map in enumerate( map_list ):
  196.                     # create our listitem, label 2 is not visible (in default skin)
  197.                     listitem = xbmcgui.ListItem( map[ 1 ], map [ 0 ] )
  198.                     # if we have a match, set our class variable
  199.                     if ( map[ 1 ] == default ):
  200.                         self.current_map = count
  201.                         self.current_maplist = self.CONTROL_MAP_LISTS[ maplist_count ]
  202.                     # add map to our list
  203.                     self.getControl( self.CONTROL_MAP_LISTS[ maplist_count ] ).addItem( listitem )
  204.                 # unlock the gui
  205.                 xbmcgui.unlock()
  206.             # fetch our map
  207.             self._fetch_map( self.current_map, self.current_maplist )
  208.  
  209.     def _fetch_map( self, map, controlId ):
  210.         # set the current map
  211.         self.current_map = map
  212.         self.current_maplist = controlId
  213.         # cancel any timer
  214.         if ( self.timer is not None ):
  215.             self.timer.cancel()
  216.             self.timer = None
  217.         # do not refresh map if not current view
  218.         if ( xbmc.getCondVisibility( "StringCompare(Window.Property(CurrentView),200) | [StringCompare(Window.Property(CurrentView),206) + IsEmpty(Window.Property(Alerts))]" ) ):
  219.             #return
  220.             # get maps url name
  221.             map = self.getControl( controlId ).getListItem( map ).getLabel2()
  222.             # make sure user can't keep selecting maps
  223.             self.loading = True
  224.             # we set our skin setting to defaultimages while downloading
  225.             self._set_maps_path()
  226.             # fetch the available map urls
  227.             maps = self.TWCClient.fetch_map_urls( map, controlId - self.CONTROL_MAP_LISTS[ 0 ] )
  228.             # fetch the images
  229.             self.maps_path, expires = self.TWCClient.fetch_images( maps )
  230.             # hack incase the weather in motion link was bogus
  231.             if ( expires < 0 and len( maps[ 1 ] ) ):
  232.                 self.maps_path, expires = self.TWCClient.fetch_images( ( maps[ 0 ], [], ) )
  233.             # we check 36 hour as it holds any alerts
  234.             self._fetch_36_forecast( False )
  235.             # now set our skin string so multi image will display images 1==success, 2==failure
  236.             self._set_maps_path( ( self.maps_path == "" ) + 1 )
  237.             # successful so set timer thread
  238.             if ( self.maps_path != "" and expires > 0 ):
  239.                 self.timer = Timer( expires, self._fetch_map, ( self.current_map, controlId, ) )
  240.                 self.timer.start()
  241.             # reset loading status
  242.             self.loading = False
  243.  
  244.     def _set_alerts( self, alerts, alertscolor, alertscount ):
  245.         # set any alerts
  246.         self.CURRENT_WINDOW.setProperty( "Alerts", alerts )
  247.         self.CURRENT_WINDOW.setProperty( "AlertsColor", alertscolor )
  248.         self.CURRENT_WINDOW.setProperty( "AlertsCount", ( "", str( alertscount ), )[ alertscount > 1 ] )
  249.         self.CURRENT_WINDOW.setProperty( "AlertsLabel", _( self.CONTROL_ALERTS_BUTTON * ( 10, 1, )[ alertscount > 1 ] ) )
  250.  
  251.     def _set_video( self, video_url ):
  252.         self.CURRENT_WINDOW.setProperty( "Video", video_url )
  253.  
  254.     def _fetch_36_forecast( self, showView=True ):
  255.         # fetch 36 hour forecast
  256.         alerts, alertscolor, alertscount, forecasts, video = self.TWCClient.fetch_36_forecast( self.CURRENT_WINDOW.getProperty( "Video" ) )
  257.         # lock the gui for faster updating
  258.         xbmcgui.lock()
  259.         try:
  260.             # set any alerts
  261.             self._set_alerts( alerts, alertscolor, alertscount )
  262.             # set video
  263.             self._set_video( video )
  264.             # enumerate thru and set the info
  265.             for day, forecast in enumerate( forecasts ):
  266.                 self.CURRENT_WINDOW.setProperty( "36Hour%dicon" % ( day + 1, ), forecast[ 1 ] )
  267.                 self.CURRENT_WINDOW.setProperty( "36Hour%dbrief" % ( day + 1, ), forecast[ 2 ] )
  268.                 self.CURRENT_WINDOW.setProperty( "36Hour%dtemptitle" % ( day + 1, ), forecast[ 3 ] )
  269.                 self.CURRENT_WINDOW.setProperty( "36Hour%dtemp" % ( day + 1, ), forecast[ 4 ] )
  270.                 self.CURRENT_WINDOW.setProperty( "36Hour%dpreciptitle" % ( day + 1, ), forecast[ 5 ] )
  271.                 self.CURRENT_WINDOW.setProperty( "36Hour%dprecip" % ( day + 1, ), forecast[ 6 ] )
  272.                 self.CURRENT_WINDOW.setProperty( "36Hour%doutlook" % ( day + 1, ), forecast[ 7 ] )
  273.                 self.CURRENT_WINDOW.setProperty( "36Hour%ddaylight" % ( day + 1, ), forecast[ 8 ] )
  274.                 self.CURRENT_WINDOW.setProperty( "36Hour%dtitle" % ( day + 1, ), forecast[ 0 ] )
  275.         except:
  276.             pass
  277.         # unlock the gui
  278.         xbmcgui.unlock()
  279.  
  280.     def _fetch_hour_forecast( self ):
  281.         # fetch hour by hour forecast
  282.         alerts, alertscolor, alertscount, headings, forecasts = self.TWCClient.fetch_hour_forecast()
  283.         try:
  284.             # set any alerts
  285.             self._set_alerts( alerts, alertscolor, alertscount )
  286.             # lock the gui for faster updating
  287.             xbmcgui.lock()
  288.             # reset list
  289.             self.getControl( self.CONTROL_HOUR_LIST ).reset()
  290.             # enumerate thru and set our heading properties
  291.             for count, heading in enumerate( headings ):
  292.                 self.CURRENT_WINDOW.setProperty( "HBHHead%d" % ( count + 1, ), heading )
  293.             # enumerate thru and set the info
  294.             for forecast in forecasts:
  295.                 listitem = xbmcgui.ListItem( forecast[ 0 ] )
  296.                 listitem.setProperty( "icon", forecast[ 1 ] )
  297.                 listitem.setProperty( "temp", forecast[ 2 ] )
  298.                 listitem.setProperty( "brief", forecast[ 3 ] )
  299.                 listitem.setProperty( "feels", forecast[ 4 ] )
  300.                 listitem.setProperty( "precip", forecast[ 5 ] )
  301.                 #listitem.setProperty( "dew", forecast[ 6 ] )
  302.                 listitem.setProperty( "humidity", forecast[ 6 ] )
  303.                 listitem.setProperty( "wind", forecast[ 7 ] )
  304.                 self.getControl( self.CONTROL_HOUR_LIST ).addItem( listitem )
  305.         except:
  306.             pass
  307.         # unlock the gui
  308.         xbmcgui.unlock()
  309.  
  310.     def _fetch_weekend_forecast( self ):
  311.         # fetch 36 hour forecast
  312.         alerts, alertscolor, alertscount, forecasts = self.TWCClient.fetch_weekend_forecast()
  313.         # lock the gui for faster updating
  314.         xbmcgui.lock()
  315.         try:
  316.             # set any alerts
  317.             self._set_alerts( alerts, alertscolor, alertscount )
  318.             # enumerate thru and set the info
  319.             for day, forecast in enumerate( forecasts ):
  320.                 self.CURRENT_WINDOW.setProperty( "Weekend%ddate" % ( day + 1, ), forecast[ 1 ] )
  321.                 self.CURRENT_WINDOW.setProperty( "Weekend%dicon" % ( day + 1, ), forecast[ 2 ] )
  322.                 self.CURRENT_WINDOW.setProperty( "Weekend%dbrief" % ( day + 1, ), forecast[ 3 ] )
  323.                 self.CURRENT_WINDOW.setProperty( "Weekend%dhightitle" % ( day + 1, ), forecast[ 4 ] )
  324.                 self.CURRENT_WINDOW.setProperty( "Weekend%dhightemp" % ( day + 1, ), forecast[ 5 ] )
  325.                 self.CURRENT_WINDOW.setProperty( "Weekend%dlowtitle" % ( day + 1, ), forecast[ 6 ] )
  326.                 self.CURRENT_WINDOW.setProperty( "Weekend%dlowtemp" % ( day + 1, ), forecast[ 7 ] )
  327.                 self.CURRENT_WINDOW.setProperty( "Weekend%dpreciptitle" % ( day + 1, ), forecast[ 8 ] )
  328.                 self.CURRENT_WINDOW.setProperty( "Weekend%dprecip" % ( day + 1, ), forecast[ 9 ] )
  329.                 self.CURRENT_WINDOW.setProperty( "Weekend%dwindtitle" % ( day + 1, ), forecast[ 10 ] )
  330.                 self.CURRENT_WINDOW.setProperty( "Weekend%dwind" % ( day + 1, ), forecast[ 11 ] )
  331.                 self.CURRENT_WINDOW.setProperty( "Weekend%duvtitle" % ( day + 1, ), forecast[ 12 ] )
  332.                 self.CURRENT_WINDOW.setProperty( "Weekend%duv" % ( day + 1, ), forecast[ 13 ] )
  333.                 self.CURRENT_WINDOW.setProperty( "Weekend%dhumiditytitle" % ( day + 1, ), forecast[ 14 ] )
  334.                 self.CURRENT_WINDOW.setProperty( "Weekend%dhumidity" % ( day + 1, ), forecast[ 15 ] )
  335.                 self.CURRENT_WINDOW.setProperty( "Weekend%dsunrisetitle" % ( day + 1, ), forecast[ 16 ] )
  336.                 self.CURRENT_WINDOW.setProperty( "Weekend%dsunrise" % ( day + 1, ), forecast[ 17 ] )
  337.                 self.CURRENT_WINDOW.setProperty( "Weekend%dsunsettitle" % ( day + 1, ), forecast[ 18 ] )
  338.                 self.CURRENT_WINDOW.setProperty( "Weekend%dsunset" % ( day + 1, ), forecast[ 19 ] )
  339.                 self.CURRENT_WINDOW.setProperty( "Weekend%doutlook" % ( day + 1, ), forecast[ 20 ] )
  340.                 self.CURRENT_WINDOW.setProperty( "Weekend%dobserved" % ( day + 1, ), forecast[ 21 ] )
  341.                 self.CURRENT_WINDOW.setProperty( "Weekend%dobservedpreciptitle" % ( day + 1, ), forecast[ 22 ] )
  342.                 self.CURRENT_WINDOW.setProperty( "Weekend%dobservedprecip" % ( day + 1, ), forecast[ 23 ] )
  343.                 self.CURRENT_WINDOW.setProperty( "Weekend%dobservedavghightitle" % ( day + 1, ), forecast[ 24 ] )
  344.                 self.CURRENT_WINDOW.setProperty( "Weekend%dobservedavghigh" % ( day + 1, ), forecast[ 25 ] )
  345.                 self.CURRENT_WINDOW.setProperty( "Weekend%dobservedavglowtitle" % ( day + 1, ), forecast[ 26 ] )
  346.                 self.CURRENT_WINDOW.setProperty( "Weekend%dobservedavglow" % ( day + 1, ), forecast[ 27 ] )
  347.                 self.CURRENT_WINDOW.setProperty( "Weekend%dobservedrecordhightitle" % ( day + 1, ), forecast[ 28 ] )
  348.                 self.CURRENT_WINDOW.setProperty( "Weekend%dobservedrecordhigh" % ( day + 1, ), forecast[ 29 ] )
  349.                 self.CURRENT_WINDOW.setProperty( "Weekend%dobservedrecordlowtitle" % ( day + 1, ), forecast[ 30 ] )
  350.                 self.CURRENT_WINDOW.setProperty( "Weekend%dobservedrecordlow" % ( day + 1, ), forecast[ 31 ] )
  351.                 ##self.CURRENT_WINDOW.setProperty( "Weekend%dalert" % ( day + 1, ), forecast[ 32 ] )
  352.                 self.CURRENT_WINDOW.setProperty( "Weekend%dday" % ( day + 1, ), forecast[ 0 ] )
  353.         except:
  354.             pass
  355.         # unlock the gui
  356.         xbmcgui.unlock()
  357.  
  358.     def _fetch_10day_forecast( self ):
  359.         # fetch hour by hour forecast
  360.         alerts, alertscolor, alertscount, headings, forecasts = self.TWCClient.fetch_10day_forecast()
  361.         # lock the gui for faster updating
  362.         xbmcgui.lock()
  363.         try:
  364.             # set any alerts
  365.             self._set_alerts( alerts, alertscolor, alertscount )
  366.             # reset list
  367.             self.getControl( self.CONTROL_10DAY_LIST ).reset()
  368.             # enumerate thru and set our heading properties
  369.             for count, heading in enumerate( headings ):
  370.                 self.CURRENT_WINDOW.setProperty( "10DayHead%d" % ( count + 1, ), heading.strip() )
  371.             # enumerate thru and set the info
  372.             for forecast in forecasts:
  373.                 listitem = xbmcgui.ListItem( forecast[ 0 ] )
  374.                 listitem.setProperty( "date", forecast[ 1 ] )
  375.                 listitem.setProperty( "icon", forecast[ 2 ] )
  376.                 listitem.setProperty( "brief", forecast[ 3 ].replace( " / ", "/" ).replace( " ", "\n" ).replace( "/", " /\n" ) )
  377.                 listitem.setProperty( "high", forecast[ 4 ] )
  378.                 listitem.setProperty( "low", forecast[ 5 ] )
  379.                 listitem.setProperty( "precip", forecast[ 6 ] )
  380.                 listitem.setProperty( "wind", forecast[ 7 ] )
  381.                 listitem.setProperty( "speed", forecast[ 8 ] )
  382.                 self.getControl( self.CONTROL_10DAY_LIST ).addItem( listitem )
  383.         except:
  384.             pass
  385.         # unlock the gui
  386.         xbmcgui.unlock()
  387.  
  388.     def _toggle_map( self ):
  389.         # toggle map
  390.         self.toggle = not self.toggle
  391.         # set the proper property
  392.         self.CURRENT_WINDOW.setProperty( "Toggle", ( "zoomed", "", )[ self.toggle ] )
  393.  
  394.     def _toggle_weekend( self ):
  395.         # toggle map
  396.         self.weekendToggle = not self.weekendToggle
  397.         # set the proper property
  398.         self.CURRENT_WINDOW.setProperty( "WeekendToggle", ( "", "details", )[ self.weekendToggle ] )
  399.  
  400.     def _get_default_view( self ):
  401.         # get our default view
  402.         defaultview = xbmc.getInfoLabel( "Skin.String(twc-defaultview)" )
  403.         self.defaultview = { "": self.CONTROL_MAP_BUTTON,
  404.                                     "200": self.CONTROL_MAP_BUTTON,
  405.                                     "201": self.CONTROL_36HOUR_BUTTON,
  406.                                     "202": self.CONTROL_HOURBYHOUR_BUTTON,
  407.                                     "203": self.CONTROL_WEEKEND_BUTTON,
  408.                                     "204": self.CONTROL_10DAY_BUTTON,
  409.                                     }[ defaultview ]
  410.  
  411.     def _set_default_view( self ):
  412.         if ( xbmc.getCondVisibility( "StringCompare(Window.Property(CurrentView),200)" ) ):
  413.             xbmc.executebuiltin( "Skin.SetString(twc-defaultmap,%s)" % ( self.getControl( self.current_maplist ).getListItem( self.current_map ).getLabel(), ) )
  414.             xbmc.executebuiltin( "Skin.SetString(twc-defaultview,200)" )
  415.         elif ( xbmc.getCondVisibility( "StringCompare(Window.Property(CurrentView),201)" ) ):
  416.             xbmc.executebuiltin( "Skin.SetString(twc-defaultview,201)" )
  417.         elif ( xbmc.getCondVisibility( "StringCompare(Window.Property(CurrentView),202)" ) ):
  418.             xbmc.executebuiltin( "Skin.SetString(twc-defaultview,202)" )
  419.         elif ( xbmc.getCondVisibility( "StringCompare(Window.Property(CurrentView),203)" ) ):
  420.             xbmc.executebuiltin( "Skin.SetString(twc-defaultview,203)" )
  421.         elif ( xbmc.getCondVisibility( "StringCompare(Window.Property(CurrentView),204)" ) ):
  422.             xbmc.executebuiltin( "Skin.SetString(twc-defaultview,204)" )
  423.         # necessary sleep to give Skin.String() time to update
  424.         xbmc.sleep( 30 )
  425.         # called so no duplicate code for setting proper self.defaultview value
  426.         self._get_default_view()
  427.         # set our new info
  428.         self._set_script_info()
  429.  
  430.     def _toggle_animated_setting( self ):
  431.         xbmc.executebuiltin( "Skin.ToggleSetting(twc-animated)" )
  432.  
  433.     def _toggle_metric_setting( self ):
  434.         xbmc.executebuiltin( "Skin.ToggleSetting(twc-metric)" )
  435.         # get our new TWCClient
  436.         self._get_client()
  437.         # clear cache
  438.         self.TWCClient.clear_cache()
  439.         # reset our view status
  440.         self._init_view_status()
  441.         # clear forecast visible properties
  442.         self._clear_forecasts()
  443.         # we check 36 hour as it holds any alerts
  444.         self._fetch_36_forecast( False )
  445.  
  446.     def _clear_forecasts( self ):
  447.         # we reset these so old info does not show when user changes metric/english setting
  448.         self.CURRENT_WINDOW.clearProperty( "36Hour1title" )
  449.         self.CURRENT_WINDOW.clearProperty( "HBHHead1" )
  450.         self.CURRENT_WINDOW.clearProperty( "Weekend1day" )
  451.         self.CURRENT_WINDOW.clearProperty( "10DayHead1" )
  452.  
  453.     def _toggle_show_alerts( self ):
  454.         xbmc.executebuiltin( "Skin.ToggleSetting(twc-show-alerts)" )
  455.  
  456.     def _choose_map_list( self, maplist ):
  457.         dialog = xbmcgui.Dialog()
  458.         choice = dialog.select( _( 450 ), [ title for title, url, tabtitle in self.TWCClient.BASE_MAPS ] )
  459.         if ( choice != -1 ):
  460.             xbmc.executebuiltin( "Skin.SetString(twc-maplist%d-category,%s)" % ( maplist - 402, self.TWCClient.BASE_MAPS[ choice ][ 0 ], ) )
  461.             xbmc.executebuiltin( "Skin.SetString(twc-maplist%d-title,%s)" % ( maplist - 402, self.TWCClient.BASE_MAPS[ choice ][ 2 ], ) )
  462.             self.current_map = None
  463.             self._fetch_map_list()
  464.  
  465.     def exit_script( self ):
  466.         # cancel any timer
  467.         if ( self.timer is not None ):
  468.             self.timer.cancel()
  469.         # TODO: remove this if dialogs get properties
  470.         # we call this as dialogs do not support properties
  471.         #self.CURRENT_WINDOW.clearProperties()
  472.         # close dialog
  473.         self.close()
  474.  
  475.     def onClick( self, controlId ):
  476.         if ( controlId == self.CONTROL_MAP_TOGGLE_BUTTON ):
  477.             self._toggle_map()
  478.         elif ( self.toggle ):
  479.             if ( controlId in self.CONTROL_MAP_LISTS and not self.loading ):
  480.                 self._fetch_map( self.getControl( controlId ).getSelectedPosition(), controlId )
  481.             elif ( controlId == self.CONTROL_MAP_BUTTON ):
  482.                 self._fetch_map_list()
  483.             elif ( controlId == self.CONTROL_36HOUR_BUTTON ):
  484.                 self._fetch_36_forecast()
  485.             elif ( controlId == self.CONTROL_HOURBYHOUR_BUTTON ):
  486.                 self._fetch_hour_forecast()
  487.             elif ( controlId == self.CONTROL_WEEKEND_BUTTON ):
  488.                 self._fetch_weekend_forecast()
  489.             elif ( controlId == self.CONTROL_10DAY_BUTTON ):
  490.                 self._fetch_10day_forecast()
  491.             elif ( controlId == self.CONTROL_WEEKEND_TOGGLE_BUTTON ):
  492.                 self._toggle_weekend()
  493.             elif ( controlId == self.CONTROL_ANIMATED_SETTING_BUTTON ):
  494.                 self._toggle_animated_setting()
  495.             elif ( controlId == self.CONTROL_METRIC_SETTING_BUTTON ):
  496.                 self._toggle_metric_setting()
  497.             elif ( controlId == self.CONTROL_SHOW_ALERTS_SETTING_BUTTON ):
  498.                 self._toggle_show_alerts()
  499.             elif ( controlId in self.CONTROL_MAPS_LIST_SETTING_BUTTONS ):
  500.                 self._choose_map_list( controlId )
  501.  
  502.     def onFocus( self, controlId ):
  503.         pass
  504.  
  505.     def onAction( self, action ):
  506.         # this try block is needed to not spam the log window with errors about no focusable item, when using a mouse
  507.         try:
  508.             # convert action to an id number
  509.             actionId = action.getId()
  510.             # perform action
  511.             if ( actionId in self.ACTION_EXIT_SCRIPT and not self.loading ):
  512.                 self.exit_script()
  513.             elif ( actionId in self.ACTION_TOGGLE_MAP and xbmc.getCondVisibility( "StringCompare(Window.Property(CurrentView),200)" ) ):
  514.                 self._toggle_map()
  515.             elif ( actionId in self.ACTION_SET_DEFAULT and self.toggle ):
  516.                 self._set_default_view()
  517.         except:
  518.             pass
  519.